package com.lamer.console.base.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lamer.console.base.entity.Menu;
import com.lamer.console.base.entity.RoleMenuRelation;
import com.lamer.console.base.service.IBaseService;
import com.lamer.console.base.service.IMenuService;
import com.lamer.console.base.service.IRoleMenuRelationService;
import com.lamer.console.common.api.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 黄诗鹏
 * @since 2021-09-25
 */
@RestController
@RequestMapping("/base")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @Autowired
    @Qualifier("MenuServiceImpl")
    private IBaseService baseService;

    @Autowired
    private IRoleMenuRelationService roleMenuRelationService;

    @GetMapping("/menu/page/")
    public ApiResult<Page<Menu>> getPage(@RequestParam("search") String search) {
        JSONObject jsonObject = JSON.parseObject(search);
        Integer number = jsonObject.getInteger("page_number");
        Integer size = jsonObject.getInteger("page_size");
        String menuName = jsonObject.getString("menuName");

        Page<Menu> page = menuService.treePage(menuName, new Page<>(number, size));
        return ApiResult.success(page);
    }

    @GetMapping("/menu/{id}")
    public ApiResult<Menu> getData(@PathVariable("id") int id) {
        Menu data = menuService.getById(id);
        return data == null ? ApiResult.failed() : ApiResult.success(data);
    }

    @PostMapping("/menu/")
    public ApiResult<Menu> insertData(@RequestBody Menu data) {
        boolean flag = menuService.save(data);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @PutMapping("/menu/")
    public ApiResult<Menu> updateData(@RequestBody Menu data) {
        boolean flag = menuService.updateById(data);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @DeleteMapping("/menu/list/")
    public ApiResult<Menu> deleteData(@RequestParam("ids") int[] ids) {
        List<Integer> collect = Arrays.stream(ids).boxed().collect(Collectors.toList());
        boolean flag = menuService.removeByIds(collect);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @GetMapping("/menu/list/{roleId}")
    public ApiResult<List<Integer>> getMenuListByRoleId(@PathVariable("roleId") int roleId){
        LambdaQueryWrapper<RoleMenuRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(RoleMenuRelation::getRoleId, roleId);
        List<RoleMenuRelation> list = roleMenuRelationService.list(queryWrapper);
        List<Integer> collect = list.stream().map(RoleMenuRelation::getMenuId).collect(Collectors.toList());
        return ApiResult.success(collect);
    }

    @PutMapping("/menu/status/{id}")
    public ApiResult<String> updateStatusById(@PathVariable("id") int id){
        boolean flag = baseService.updateStatusById(id);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @GetMapping("/menu/list/parent/")
    public ApiResult<List<Menu>> getParentMenuList(){
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Menu::getIsParent, 1);
        List<Menu> list = menuService.list(queryWrapper);
        return ApiResult.success(list);
    }

    @GetMapping("/menu/tree/")
    public ApiResult<List<Menu>> getMenuTree(){
        List<Menu> list = menuService.treeList();
        return ApiResult.success(list);
    }

    @PutMapping("/menu/list/allocate/")
    public ApiResult<String> allocateMenuListByRoleId(@RequestParam("roleId") int roleId, @RequestParam("menuIds") int[] menuIds){
        List<Integer> collect = Arrays.stream(menuIds).boxed().collect(Collectors.toList());
        boolean flag = roleMenuRelationService.allocateAuthority(roleId, collect);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

}

